package org.dentalcard.ui.schedule.tools;

import org.dentalcard.entity.WorkDay;

import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @user: Vladimir Chernishov
 * @date: 25.12.11
 * @time: 18:40
 */
public class WorkScheduleBuilder {
    
    private WorkScheduleModel model;
    private JTable table;

    public WorkScheduleBuilder(List<WorkDay> workDays) {
        
        buildModel(workDays);
    }

    private void buildModel(List<WorkDay> workDays) {
        model = new WorkScheduleModel();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
        
        if(firstDay == Calendar.MONDAY){
            firstDay = 7;
        } else{
            firstDay--;
        }
        
        for(int i = 1 ; i < firstDay; i++){
            model.addWorkDay(new WorkScheduleDay());
        }

        Iterator<WorkDay> workDaysIt = workDays.iterator();
        WorkDay workDay = workDaysIt.hasNext() ? workDaysIt.next() : null;

        for(int lastYearDay = calendar.getMaximum(Calendar.DAY_OF_YEAR), day = 1; day < lastYearDay; day++){
            calendar.set(Calendar.DAY_OF_YEAR, day);
            WorkScheduleDay workScheduleDay = new WorkScheduleDay();
            Date time = calendar.getTime();
            workScheduleDay.setDate(time);
            if(compareDates(workDay, time) == 0){
                workScheduleDay.setDay(workDay);
                if(workDaysIt.hasNext()){
                    workDay = workDaysIt.next();
                } else {
                    workDay = null;
                }

            }
            if(day == 1 && calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY){
                int emptyDays;
                if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
                    emptyDays = 6;
                } else {
                    emptyDays = calendar.get(Calendar.DAY_OF_WEEK) -1;
                }
                for(int i = 0; i < emptyDays; i++){
                    model.addWorkDay(new WorkScheduleDay());
                }
                
            }
            
            model.addWorkDay(workScheduleDay);
        }
    }

    private int compareDates(WorkDay workDay, Date time) {
        if(workDay == null){
            return -1;
        }
        Date day = workDay.getDay();
        if(time.getMonth() == day.getMonth()){
            if(time.getDate() == day.getDate()){
                return 0;
            }
        }
        return 1;
    }


    public JTable buildTable() {
        if(table == null){
            table = new JTable(model);
            WorkScheduleRenderer cellRenderer = new WorkScheduleRenderer();
            for(int i = 0; i < table.getColumnCount(); i++){
                table.getColumnModel().getColumn(i).setCellRenderer(cellRenderer);
            }
            table.setRowHeight(50);
        }
        return table;
    }

}
